<?php
class Order_Model_OrderStatistics
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Order_Model_DbTable_OrderStatistics();
	}
	
	/**
	*add DB Adapter
	*/
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	
	/**
	*Create A Record  
	*@param rowSet $row Order's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Order's data 
	*@param int $wh_id 
	*@return boolean
	*/
	public  function update($row,$id){	
		$where = $this->_table->getAdapter()->quoteInto("Order_id= ?", $id);
		return $this->_table->update($row, $where);		
	}
	
	public function updateByField($row,$field,$value)
	{ 
		$where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
		return $this->_table->update($row, $where);
	}
		
	/**
	*Delete One Row
	*@return boolean
	*/
	public function deleteByField($field,$value){	
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value); 
		return $this->_table->delete($where);		
	}

    public function getByCond($condition=array(),$type='*',$pageSize=20,$page=1,$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition['user_id']) && $condition['user_id']!=''){
            $select->where('user_id=?', $condition['user_id']);
        }
        if(isset($condition['ot_class']) && $condition['ot_class']!==''){
            $select->where('ot_class=?', $condition['ot_class']);
        }
        if(isset($condition["dateFrom"]) && $condition["dateFrom"] ){        	
			 $select->where('ot_date >=?', $condition["dateFrom"]);			         	
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"]){
            $select->where(" ot_date <=?",$condition["dateTo"]);
        }
        
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	public function getByfield($field,$value){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchRow($where);
	}
	
	/**
	 * 根据条件返回一条数据
	 *
	 */
	public function getRowByCond($condition){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("1 =?",1);
		if(isset($condition['user_id']) && $condition['user_id']!=''){
            $select->where('user_id=?', $condition['user_id']);
        }
        if(isset($condition['ot_class']) && $condition['ot_class']!==''){
            $select->where('ot_class=?', $condition['ot_class']);
        }
        if( isset($condition["ot_date"]) && $condition['ot_date']!='' ){
        	 $select->where('ot_date=?', $condition['ot_date']);
        }
        $sql=$select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql);        
	}
	
	/**
	 * 根据条件更新一条数据
	 *
	 */
	public function updateByCond($row,$cond)
	{ 
		$adapter = $this->_table->getAdapter();
		$where = $adapter->quoteInto(" user_id= ?", $cond['user_id']);
		$where .= $adapter->quoteInto(" and ot_class= ?", $cond['ot_class']);
		$where .= $adapter->quoteInto(" and ot_date= ?", $cond['ot_date']);
		return $this->_table->update($row, $where);
	}
	
	
}